#include "gd32f30x.h"
#include "string.h"
#include "stdint.h"
#include "stdio.h"
#include "DevicesCAN.h"
#include "saeJ1939.h"
#include "taskMessage.h"
#include "taskSaeJ1939.h"
#include "taskMrDemo.h"


char g_ucCanxTxBuff[32] = "Hello Canx\r\n";


/* 任务句柄 */
MrTaskType *g_ptypeMrTaskAdd;

/* 消息队列句柄 */
MrMessageType *g_ptypeMrMessageDemo;

/* 定时器句柄 */
MrTimerType *g_ptypeMrTimerDemo;


void vMrTaskAdd(uint32_t uiParameters)
{
//    cCanxSendDatas(CAN0, 3, g_ucCanxTxBuff, strlen(g_ucCanxTxBuff));
//    cCanxSendDatas(CAN1, 3, g_ucCanxTxBuff, strlen(g_ucCanxTxBuff));

    while(1)
    {
        ++uiParameters;

        cMessageSend(g_ptypeMrMessageDemo, &uiParameters, 0);

        vMrDelayMs(NULL, 100);
    }
}


void vMrTimerDemo(uint32_t uiParameters)
{
    uiParameters += uiParameters;

    cJ1939PgnSend(0x002600, 0x56);
}


void vMrRtosInit(void)
{
    /* 创建消息队列 */
    cMrMessageCreate(&g_ptypeMrMessageDemo,     MR_MESSAGE_TYPE_FIFO, sizeof(uint32_t), 8, 0);


    /* 创建任务 */
    cMrTaskCreate(&g_ptypeMrTaskAdd,        vMrTaskAdd,         "Add",          2,  256,    100000);
    cMrTaskCreate(NULL,                     vMrTaskAdd,         "Add 2",        2,  256,    100000);

    cMrTaskCreate(&g_ptypeMrTaskMessage,    vMrTaskMessage,     "Message",      4,  256,    200000);
    cMrTaskCreate(&g_ptypeMrTaskJ1939,      vMrTaskJ1939,       "SAE-J1939",    5,  256,    200000);


    /* 创建定时器 */
    cMrTimerCreate(&g_ptypeMrTimerDemo,     vMrTimerDemo,       (uint32_t)&g_ptypeMrTimerDemo,  1000);
    /* 启动定时器 */
    cMrTimerStart(g_ptypeMrTimerDemo,       MrTimerCirculation, g_ptypeMrTimerDemo->duration);


    /* 启动RTOS运行 */
    cMrTaskStart();
}
